using HKB.Common;
using HKB.Data.Dal;
using HKB.Data.Model;
using System;
using System.Collections.Generic;
using System.Data;
namespace HKB.Data.Bll
{
	public class AccountAoucherItem
	{
		private readonly HKB.Data.Dal.AccountAoucherItem dal = new HKB.Data.Dal.AccountAoucherItem();
		public int GetMaxId()
		{
			return this.dal.GetMaxId();
		}
		public bool Exists(int AccountAItemID)
		{
			return this.dal.Exists(AccountAItemID);
		}
		public int Add(HKB.Data.Model.AccountAoucherItem model)
		{
			return this.dal.Add(model);
		}
		public int AddTransaction(HKB.Data.Model.AccountAoucherItem model, Conn conn)
		{
			return this.dal.AddTransaction(model, conn);
		}
		public bool Update(HKB.Data.Model.AccountAoucherItem model)
		{
			return this.dal.Update(model);
		}
		public bool Delete(int AccountAItemID)
		{
			return this.dal.Delete(AccountAItemID);
		}
		public bool DeleteList(string AccountAItemIDlist)
		{
			return this.dal.DeleteList(AccountAItemIDlist);
		}
		public HKB.Data.Model.AccountAoucherItem GetModel(int AccountAItemID)
		{
			return this.dal.GetModel(AccountAItemID);
		}
		public HKB.Data.Model.AccountAoucherItem GetModelByCache(int AccountAItemID)
		{
			string cacheKey = "AccountAoucherItemModel-" + AccountAItemID;
			object obj = DataCache.GetCache(cacheKey);
			if (obj == null)
			{
				try
				{
					obj = this.dal.GetModel(AccountAItemID);
					if (obj != null)
					{
						int configInt = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, obj, System.DateTime.Now.AddMinutes((double)configInt), System.TimeSpan.Zero);
					}
				}
				catch
				{
				}
			}
			return (HKB.Data.Model.AccountAoucherItem)obj;
		}
		public DataSet GetList(string strWhere)
		{
			return this.dal.GetList(strWhere);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			return this.dal.GetList(Top, strWhere, filedOrder);
		}
		public System.Collections.Generic.List<HKB.Data.Model.AccountAoucherItem> GetModelList(string strWhere)
		{
			DataSet list = this.dal.GetList(strWhere);
			return this.DataTableToList(list.Tables[0]);
		}
		public System.Collections.Generic.List<HKB.Data.Model.AccountAoucherItem> DataTableToList(DataTable dt)
		{
			System.Collections.Generic.List<HKB.Data.Model.AccountAoucherItem> list = new System.Collections.Generic.List<HKB.Data.Model.AccountAoucherItem>();
			int count = dt.Rows.Count;
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					HKB.Data.Model.AccountAoucherItem accountAoucherItem = this.dal.DataRowToModel(dt.Rows[i]);
					if (accountAoucherItem != null)
					{
						list.Add(accountAoucherItem);
					}
				}
			}
			return list;
		}
		public DataSet GetAllList()
		{
			return this.GetList("");
		}
		public int GetRecordCount(string strWhere)
		{
			return this.dal.GetRecordCount(strWhere);
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
		}
		public int AddAccountAoucherItem(int addInfo, decimal borrowMoney, decimal leanMoney, string summary, string accountSubjectName)
		{
			int result = 0;
			AccountSubject accountSubject = new AccountSubject();
			HKB.Data.Model.AccountSubject modelByName = accountSubject.GetModelByName(accountSubjectName);
			if (modelByName != null)
			{
				HKB.Data.Model.AccountAoucherItem accountAoucherItem = new HKB.Data.Model.AccountAoucherItem();
				accountAoucherItem.AccountAoucherID = new int?(addInfo);
				accountAoucherItem.AAItemAccountSID = new int?(modelByName.AccountSubjectID);
				if (borrowMoney > 0m && leanMoney == 0m)
				{
					accountAoucherItem.BorrowPrice = new decimal?(borrowMoney);
					accountAoucherItem.LoanPrice = new decimal?(leanMoney);
				}
				else
				{
					if (borrowMoney.Equals(0m) && leanMoney > 0m)
					{
						accountAoucherItem.BorrowPrice = new decimal?(borrowMoney);
						accountAoucherItem.LoanPrice = new decimal?(leanMoney);
					}
				}
				accountAoucherItem.AAItemSummary = summary;
				result = this.dal.Add(accountAoucherItem);
			}
			return result;
		}
		public int AccountAoucherItemAddBySell(int aainfoID, decimal ReceiptMoney, decimal MoneyGoods1, decimal Deliver_TotalPrice, decimal Invoice_TotalPrice, decimal fee)
		{
			int result = 0;
			if (aainfoID > 0)
			{
				if (ReceiptMoney > 0m)
				{
					result = this.AddAccountAoucherItem(aainfoID, ReceiptMoney, 0m, "销售出库单", "应收账款");
				}
				if (MoneyGoods1 > 0m)
				{
					result = this.AddAccountAoucherItem(aainfoID, 0m, MoneyGoods1, "销售出库单", "商品销售收入");
				}
				if (Deliver_TotalPrice > 0m)
				{
					result = this.AddAccountAoucherItem(aainfoID, 0m, Deliver_TotalPrice, "销售出库单", "运输费");
				}
				if (Invoice_TotalPrice > 0m)
				{
					result = this.AddAccountAoucherItem(aainfoID, 0m, Invoice_TotalPrice, "销售出库单", "发票费");
				}
			}
			return result;
		}
		public int AddAccountAoucherItemTransaction(int addInfo, decimal borrowMoney, decimal leanMoney, string summary, string accountSubjectName, Conn conn)
		{
			int result = 0;
			AccountSubject accountSubject = new AccountSubject();
			HKB.Data.Model.AccountSubject modelByName = accountSubject.GetModelByName(accountSubjectName);
			if (modelByName != null)
			{
				HKB.Data.Model.AccountAoucherItem accountAoucherItem = new HKB.Data.Model.AccountAoucherItem();
				accountAoucherItem.AccountAoucherID = new int?(addInfo);
				accountAoucherItem.AAItemAccountSID = new int?(modelByName.AccountSubjectID);
				if (borrowMoney > 0m && leanMoney == 0m)
				{
					accountAoucherItem.BorrowPrice = new decimal?(borrowMoney);
					accountAoucherItem.LoanPrice = new decimal?(leanMoney);
				}
				else
				{
					if (borrowMoney.Equals(0m) && leanMoney > 0m)
					{
						accountAoucherItem.BorrowPrice = new decimal?(borrowMoney);
						accountAoucherItem.LoanPrice = new decimal?(leanMoney);
					}
				}
				accountAoucherItem.AAItemSummary = summary;
				result = this.dal.AddTransaction(accountAoucherItem, conn);
			}
			return result;
		}
		public int AccountAoucherItemAddBySellTransaction(int aainfoID, decimal ReceiptMoney, decimal MoneyGoods1, decimal Deliver_TotalPrice, decimal Invoice_TotalPrice, decimal fee, Conn conn)
		{
			int result = 0;
			if (aainfoID > 0)
			{
				if (ReceiptMoney > 0m)
				{
					result = this.AddAccountAoucherItemTransaction(aainfoID, ReceiptMoney, 0m, "销售出库单", "应收账款", conn);
				}
				if (MoneyGoods1 > 0m)
				{
					result = this.AddAccountAoucherItemTransaction(aainfoID, 0m, MoneyGoods1, "销售出库单", "商品销售收入", conn);
				}
				if (Deliver_TotalPrice > 0m)
				{
					result = this.AddAccountAoucherItemTransaction(aainfoID, 0m, Deliver_TotalPrice, "销售出库单", "运输费", conn);
				}
				if (Invoice_TotalPrice > 0m)
				{
					result = this.AddAccountAoucherItemTransaction(aainfoID, 0m, Invoice_TotalPrice, "销售出库单", "发票费", conn);
				}
			}
			return result;
		}
	}
}
